package interfaces.spawnMenu.costumize;

import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.spawnMenu.AbsSpawnMenu;
import interfaces.spawnMenu.SpawnMenuWindow;
import interfaces.spawnMenu.costumize.hunters.HunterSelectContent;
import interfaces.spawnMenu.costumize.items.ItemSelectContent;
import interfaces.spawnMenu.costumize.weapons.WeaponSelectContent;
import interfaces.superWidgets.MoneyListener;
import interfaces.superWidgets.StaticContent;
import interfaces.superWidgets.tabs.TabSwitchContent;

import java.util.ArrayList;
import java.util.List;

import logic.common.player.Player;
import logic.common.team.Team;
import logic.nodes.nodeSettings.upgrades.UpgradableSettings;
import logic.ships.hunter.Hunter;
import fileHandling.language.Lang;
import fileHandling.language.interfaces.SpawnMenuText;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Dimension;

public class CustomizeWindow extends SpawnMenuWindow implements MoneyListener {
	
	private WeaponSelectContent weaponSelectContent;
	private ItemSelectContent itemContent;
	private HunterSelectContent hunterSelectContent;
	
	private List<CustomizeContainer<?>> teamContainer;
	private TabSwitchContent tabSwitchContent;
	private Player player;
	
	private ImageFont buttonFont;
	
	public CustomizeWindow(Team team, Team enemyTeam, AbsSpawnMenu spawnMenu) {
		super(SpawnMenuText.Customize, team, enemyTeam, spawnMenu);
		
		player = spawnMenu.getGame().getCurrentPlayer();
		
		buttonFont = GUISource.middleFont;
		content.setLayoutManager(new StaticLayout());
		layout();
		
		init();
		
		layout();
	}
	
	private void init() {
		tabSwitchContent = new TabSwitchContent(content.getWidth(), content.getHeight(), buttonFont);
		content.addWidget(tabSwitchContent);
		
		teamContainer = new ArrayList<CustomizeContainer<? extends UpgradableSettings>>();
		Dimension contentSize = tabSwitchContent.getSwitchContentSize();
		int contentWidth = contentSize.getWidth();
		int contentHeight = contentSize.getHeight();
		
		hunterSelectContent = new HunterSelectContent(team, contentWidth, contentHeight, this);
		weaponSelectContent = new WeaponSelectContent(team, contentWidth, contentHeight, player);
		itemContent = new ItemSelectContent(team, contentWidth, contentHeight, spawnMenu);
		
		addSwitchContent(SpawnMenuText.Select_your_Hunter, hunterSelectContent);
		addSwitchContent(SpawnMenuText.Select_Items, itemContent);
		addSwitchContent(SpawnMenuText.Select_your_Weapons, weaponSelectContent);
		
		teamContainer.add(hunterSelectContent);
		teamContainer.add(weaponSelectContent);
		teamContainer.add(itemContent);
		
		tabSwitchContent.switchContent(hunterSelectContent);
	}
	
	private void addSwitchContent(Lang text, StaticContent content) {
		LanguageButton button = new LanguageButton(text, buttonFont);
		tabSwitchContent.addSwitchContent(button, content);
	}
	
	@Override
	public void changeTeam(Team newTeam) {
		enemyTeam = team;
		team = newTeam;
		
		content.removeAllWidgets();
		
		init();
	}
	
	@Override
	public void changeHunter(Hunter hunter) {
		this.hunter = hunter;
		if(hunter != null) {
			Player p = spawnMenu.getGame().getCurrentPlayer();
			if(p.getHunter() != hunter) p.changeHunter(hunter);
		}
		
		StaticContent lastContent = tabSwitchContent.getActiveContent();
		for(CustomizeContainer<? extends UpgradableSettings> teamCont : teamContainer) {
			if(teamCont != null) {
				tabSwitchContent.switchContent(teamCont);
				teamCont.changeHunter(hunter);
			}
		}
		tabSwitchContent.switchContent(lastContent);
		spawnMenu.changeSelectedHunter(hunter);
	}
	
	public TabSwitchContent getTabSwitchContent() { return tabSwitchContent; }
	
	@Override
	public void updateMoney(int newMoney) {
		for(CustomizeContainer<?> content : teamContainer) {
			content.updateMoney(newMoney);
		}
	}
	
	public Player getPlayer() { return player; }
	
	public void restore() {
		for(CustomizeContainer<?> content : teamContainer) {
			content.restore();
		}
	}
	
	public void addWeaponSwitchTipListener(IButtonPressedListener listener) {
		tabSwitchContent.addSwitchListener(weaponSelectContent, listener);
	}
	
	public WeaponSelectContent getWeaponSelectContent() { return weaponSelectContent; }
	
	public HunterSelectContent getHunterSelectContent() { return hunterSelectContent; }
}